package com.aletto.general_store.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.MockDataSource;
import com.aletto.general_store.data.model.Address;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.presenters.core.NetResumeSingleTask;
import com.aletto.general_store.ui.adapters.AddressListAdapter;
import com.aletto.general_store.utils.XHelper;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.QuickList;
import com.sisyphus.spinach.ui.configs.impl.DefaultTemplate;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 地址管理
 */
public class AddressManageActivity extends AppCompatActivity {

    public static Intent newIntent(Context context){
        Intent intent = new Intent(context, AddressManageActivity.class);
//        context.startActivity(intent);
        intent.putExtra("TYPE", 2);
        return intent;
    }

    private int type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_manage);
        XHelper.initTitle(this, R.string.title_address_manage);

        Intent intent = getIntent();
        type = intent.getIntExtra("TYPE", 1);

        findViewById(R.id.tv_add_address).setOnClickListener(v->
                startActivity(new Intent(v.getContext(), AddressAddActivity.class)));

        presenter().execute(getLifecycle());
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter().execute(getLifecycle());
    }

    private DefaultTemplate<List<Address>> presenter(){
        return new DefaultTemplate<>(findViewById(R.id.recycler_view)) {

            @Override
            public INetTask.View<List<Address>> view() {
                AddressListAdapter adapter = new AddressListAdapter(type);
                ((RecyclerView) mParent).setAdapter(adapter);
                return new QuickList<>(adapter);
            }

            @Override
            public INetTask.Presenter<List<Address>, INetTask.View<List<Address>>> presenter() {
                return new NetSingleTask<>(DataRepository.getInstance().addressList());
            }
        };
    }

}
